package web.fabrimind.sm.service.impl;

import java.util.List;
import web.fabrimind.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import web.fabrimind.sm.mapper.TeamMemberMapper;
import web.fabrimind.sm.domain.TeamMember;
import web.fabrimind.sm.service.ITeamMemberService;
import web.fabrimind.sm.mapper.ProductionTeamMapper;
import web.fabrimind.sm.domain.ProductionTeam;

/**
 * 团队成员Service业务层处理
 *
 * @author zhihui
 * @date 2025-03-30
 */
@Service
public class TeamMemberServiceImpl implements ITeamMemberService
{
    @Autowired
    private TeamMemberMapper teamMemberMapper;

    @Autowired
    private ProductionTeamMapper productionTeamMapper;

    /**
     * 查询团队成员
     *
     * @param memberId 团队成员主键
     * @return 团队成员
     */
    @Override
    public TeamMember selectTeamMemberByMemberId(Long memberId)
    {
        return teamMemberMapper.selectTeamMemberByMemberId(memberId);
    }

    /**
     * 查询团队成员列表
     *
     * @param teamMember 团队成员
     * @return 团队成员
     */
    @Override
    public List<TeamMember> selectTeamMemberList(TeamMember teamMember)
    {
        return teamMemberMapper.selectTeamMemberList(teamMember);
    }

    /**
     * 查询可用成员列表（包括所有用户和已有团队成员）
     *
     * @param teamMember 团队成员
     * @return 可用成员集合
     */
    @Override
    public List<TeamMember> selectAvailableMemberList(TeamMember teamMember)
    {
        return teamMemberMapper.selectAvailableMemberList(teamMember);
    }

    /**
     * 新增团队成员
     *
     * @param teamMember 团队成员
     * @return 结果
     */
    @Override
    public int insertTeamMember(TeamMember teamMember)
    {
        teamMember.setCreateTime(DateUtils.getNowDate());
        int rows = teamMemberMapper.insertTeamMember(teamMember);

        // 如果新添加的成员是组长，则更新其他成员为非组长
        if ("1".equals(teamMember.getIsLeader()) && teamMember.getTeamId() != null && teamMember.getUserId() != null)
        {
            updateTeamLeader(teamMember.getTeamId(), teamMember.getUserId());
        }

        return rows;
    }

    /**
     * 修改团队成员
     *
     * @param teamMember 团队成员
     * @return 结果
     */
    @Override
    public int updateTeamMember(TeamMember teamMember)
    {
        teamMember.setUpdateTime(DateUtils.getNowDate());
        int rows = teamMemberMapper.updateTeamMember(teamMember);

        // 如果修改的成员是组长，则更新其他成员为非组长
        if ("1".equals(teamMember.getIsLeader()) && teamMember.getTeamId() != null && teamMember.getUserId() != null)
        {
            updateTeamLeader(teamMember.getTeamId(), teamMember.getUserId());
        }

        return rows;
    }

    /**
     * 批量删除团队成员
     *
     * @param memberIds 需要删除的团队成员主键
     * @return 结果
     */
    @Override
    public int deleteTeamMemberByMemberIds(Long[] memberIds)
    {
        return teamMemberMapper.deleteTeamMemberByMemberIds(memberIds);
    }

    /**
     * 删除团队成员信息
     *
     * @param memberId 团队成员主键
     * @return 结果
     */
    @Override
    public int deleteTeamMemberByMemberId(Long memberId)
    {
        return teamMemberMapper.deleteTeamMemberByMemberId(memberId);
    }

    /**
     * 查询所属客户列表（不分页）
     */
    @Override
    public List<TeamMember> selectUserList(TeamMember teamMember) {
        return teamMemberMapper.selectUserList(teamMember);
    }

    /**
     * 查询所属团队列表（不分页）
     */
    @Override
    public List<TeamMember> selectTeamList(TeamMember teamMember) {
        return teamMemberMapper.selectTeamList(teamMember);
    }

    /**
     * 更新团队组长
     *
     * @param teamId 团队ID
     * @param userId 用户ID
     */
    private void updateTeamLeader(Long teamId, Long userId)
    {
        teamMemberMapper.updateTeamLeader(teamId, userId);
    }
}